<template>
  <!-- 产品分组 -->
  <div class="app-container">
    <custom-container ref="customContainerRef" :config="mainConfig" />
    <!-- 新增或修改产品信息 -->
    <setForm ref="setFormRef" @changeList="handleQuery" />
  </div>
</template>
<script lang="ts" setup>
import { Edit, Delete, Plus } from '@element-plus/icons-vue'
import { ref, defineAsyncComponent, computed } from 'vue'
import { ElMessage, ElMessageBox, ElButton } from 'element-plus'
import { DictDirection } from '@/store/modules/dicts'
import { listGroup, delGroup } from '@/api/basicConfig/productsGroup'

const setForm = defineAsyncComponent(() => import('./compontents/setForm.vue'))
// 非单个禁用
const
  single = ref(true),
  // 非多个禁用
  multiple = ref(true),
  queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    groupName: undefined,
    status: undefined,
    remarks: undefined,
  }),
  customContainerRef = ref(null),
  setFormRef = ref(null)
const handleQuery = () => {
  queryParams.value.pageNum = 1
  customContainerRef.value.getList()
}
let ids = []
// 多选框选中数据
const handleSelectionChange = (rows: { [key: string]: any }[]) => {
  const rowsList = JSON.parse(JSON.stringify(rows))
  ids = rowsList.map((item) => item.id)
  single.value = rowsList.length != 1
  multiple.value = !rowsList.length
}
// 添加产品
const handleAdd = () => {
  setFormRef.value.open = true
  setFormRef.value.title = '添加产品'
}
// 删除产品
const handleDelete = (row: { [key: string]: any }) => {
  const id = row.id || ids
  console.log(id)
  ElMessageBox.confirm(`"是否确认删除产品编号为"${ id }"的数据项?"`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  .then(() => {
    delGroup(id)
    handleQuery()
    ElMessage.success('删除成功')
  })
  .catch((error) => {
    if (error == 'cancel') {
      ElMessage.info('取消删除')
    }
  })
}
// 修改产品
const handleUpdate = (row: { [key: string]: any }) => {
  const id = row.id || ids
  setFormRef.value.open = true
  setFormRef.value.title = '修改产品'
  console.log(row)
  setFormRef.value.getDetail(id)
}

const mainConfig = computed({
  get() {
    return {
      query: {
        fields: [
          {
            label: '分组名称',
            prop: 'groupName',
            option: {
              placeholder: '请输入分组名称',
            },
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            option: {
              placeholder: '请选择状态',
              dictInfo: {
                dict: DictDirection.通用状态,
              },
            },
          },
        ],
        queryParams: queryParams.value,
      },
      btns: [
        {
          title: '新增',
          icon: Plus,
          // directive: [['hasPermi', ['system:dict:add']]],
          onClick: handleAdd,
        },
        {
          title: '修改',
          icon: Edit,
          disabled: single.value,
          // directive: [['hasPermi', ['system:dict:add']]],
          onClick: handleUpdate,
        },
        {
          title: '删除',
          icon: Delete,
          disabled: multiple.value,
          // directive: [['hasPermi', ['system:dict:remove']]],
          onClick: handleDelete,
        },
      ],
      table: {
        columns: [
          {
            type: 'selection',
            width: '55',
          },
          {
            label: '分组名称',
            prop: 'groupName',
            align: 'center',
          },
          {
            label: '状态',
            prop: 'status',
            align: 'center',
            autoFormatter: {
              dict: DictDirection.通用状态,
              rowKey: 'status',
            },
          },
          {
            label: '备注',
            prop: 'remark',
            align: 'center',
          },
          {
            label: '操作',
            align: 'center',
            showOverflowTooltip: false,
            render: (h, params) => {
              const buttons = [
                {
                  title: '修改',
                  icon: Edit,
                  clickFun: handleUpdate,
                },
                {
                  title: '删除',
                  icon: Delete,
                  clickFun: handleDelete,
                },
              ]
              return buttons.map((el) => h(
                ElButton,
                {
                  text: true,
                  type: 'primary',
                  icon: el.icon,
                  onClick: () => {
                    el.clickFun(params.row)
                  },
                },
                {
                  default: () => el.title,
                },
              ))
            },
          },
        ],
        event: {
          onSelectionChange: (rows) => {
            handleSelectionChange(rows)
          },
        },
      },
      api: listGroup,
    }
  },
  set(val) {
    return val
  },
})
</script>
